package api

import (
	"gateway/basic/config"
	__ "gateway/basic/proto"
	"gateway/handler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

func CreateOrder(c *gin.Context) {
	var req request.CreateOrderReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "参数错误",
		})
		return
	}
	_, err := config.DacheClient.CreateOrder(c, &__.CreateOrderReq{
		OrderSn:     req.OrderSn,
		PassagerId:  int64(req.PassagerId),
		DriverId:    int64(req.DriverId),
		OrderStatus: int64(req.OrderStatus),
		PayType:     int64(req.PayType),
		PayStatus:   int64(req.PayStatus),
		PayPrice:    float32(req.PayPrice),
		Kilo:        float32(req.Kilo),
		CarType:     req.CarType,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "客户端连接失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "订单创建成功",
		"data": nil,
	})
}

func UpdateOrder(c *gin.Context) {
	var req request.UpdateOrderReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "参数错误",
		})
		return
	}
	_, err := config.DacheClient.DriverUpdateOrder(c, &__.DriverUpdateOrderReq{
		OrderId:     int64(req.Id),
		OrderStatus: int64(req.OrderStatus),
		OrderMode:   req.OrderMode,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "客户端连接失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "修改订单状态成功",
		"data": nil,
	})
}

func GetOrderList(c *gin.Context) {
	var req request.GetOrderListReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "参数错误",
		})
		return
	}
	order, err := config.DacheClient.GetMyOrder(c, &__.GetMyOrderReq{
		OrderStatus: int64(req.OrderStatus),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "客户端连接失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "订单列表展示成功",
		"data": order,
	})
}

func GetOrderDetail(c *gin.Context) {
	var req request.GetOrderDetailReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "参数错误",
		})
		return
	}
	order, err := config.DacheClient.GetOrderDetail(c, &__.GetOrderDetailReq{
		Id: int64(req.Id),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "客户端连接失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "订单详情查询成功",
		"data": order,
	})
}
